use std::sync::Mutex;

use honeybee::mongodb::bson::Document;
use honeybee::mongodb::Client;
use honeybee::mongodb::Collection;
use once_cell::sync::Lazy;

static TEMP: Lazy<Mutex<Option<Client>>> = Lazy::new(Mutex::default);

struct Collections {
    client: Client,
    account: Collection<Document>,
    role: Collection<Document>,
    uid: Collection<Document>,
}

static COLLECTIONS: Lazy<Collections> = Lazy::new(|| {
    let client = TEMP.lock().unwrap().take().unwrap();
    Collections {
        account: client.database("center").collection("account"),
        role: client.database("game").collection("role"),
        uid: client.database("center").collection("uid"),
        client,
    }
});

pub fn set(client: Client) {
    *TEMP.lock().unwrap() = Some(client);
}

#[allow(dead_code)]
pub fn client() -> &'static Client {
    &COLLECTIONS.client
}

pub fn account() -> &'static Collection<Document> {
    &COLLECTIONS.account
}

pub fn role() -> &'static Collection<Document> {
    &COLLECTIONS.role
}

pub fn uid() -> &'static Collection<Document> {
    &COLLECTIONS.uid
}
